import util from "../../utils/util.js";
import * as echarts from '../../ec-canvas/echarts';

const app = getApp();
const typeObj = {
  totalTax: { typeName: '税收总额', color: '#8887FF', unit: '万元' }, //税收总额
  parkEnterpriseTaxNewNum: { typeName: '新增户税收', color: '#FF6262', unit: '万元' }, //新增户税收
  parkEnterpriseNum: {
      typeName: '企业注册数',
      color: '#1166FF',
      unit: '户'
  }, //企业注册数
  parkEnterpriseTaxNum: { typeName: '纳税企业数', color: '#00C9FF', unit: '户' }, //纳税企业数
  parkEnterpriseQualityNum: { typeName: '新增质量户', color: '#FF9F24', unit: '户' } //新增质量户
}
const typeArray=Object.keys(typeObj)

Page({
  data: {
    ecBar: {
      lazyLoad: true
    },
    date: '1997-01',//默认起始时间  
    date2: '2018-01',//默认结束时间 
    date3: '2018',//默认结束时间 
    tabType:'morth',
    queryType:'1',
    homeParkStatistic:{},
    compelteRateRes:{
      parkEnterpriseNum:{
        currentData: 5126,
        lastData: 6593,
        order: "asc",
        orderByStr: null,
        pageNum: 1,
        pageSize: 10,
        rate: -0.22,
        sort: null,
      },
      parkEnterpriseQualityNum:{
        currentData: 5126,
        lastData: 6593,
        order: "asc",
        orderByStr: null,
        pageNum: 1,
        pageSize: 10,
        rate: -0.22,
        sort: null,
      },
      parkEnterpriseTaxNewNum:{
        currentData: 5126,
        lastData: 6593,
        order: "asc",
        orderByStr: null,
        pageNum: 1,
        pageSize: 10,
        rate: -0.22,
        sort: null,
      },
      parkEnterpriseTaxNum:{
        currentData: 5126,
        lastData: 6593,
        order: "asc",
        orderByStr: null,
        pageNum: 1,
        pageSize: 10,
        rate: -0.22,
        sort: null,
      },
      totalTax:{
        currentData: 5126,
        lastData: 6593,
        order: "asc",
        orderByStr: null,
        pageNum: 1,
        pageSize: 10,
        rate: -0.22,
        sort: null,
      }
    }
  },
  onShow:function(){
   this.getParkStatistic();
   this.getYearOnYearAndCompletionRate();
   this.echartsComponnet = this.selectComponent('#mychart-dom-line');
   this.init_echarts()
  },
  init_echarts: function () {
    this.echartsComponnet.init((canvas, width, height) => {
      // 初始化图表
      const Chart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      Chart.setOption(this.getBarOption());
      // 注意这里一定要返回 chart 实例，否则会影响事件处理等
      return Chart;
    });
  },
// 时间段选择  
bindDateChange(e) {
  let that = this;
  console.log(e.detail.value)
  that.setData({
    date: e.detail.value,
  })
},
bindDateChange2(e) {
  let that = this;
  that.setData({
    date2: e.detail.value,
  })
},
changeTab(e){
  let _this = this;
  _this.setData({
    tabType: e.target.dataset.type,
    queryType: e.target.dataset.queryType
  })
},
getYearOnYearAndCompletionRate:function(){
  let _this = this;
  wx.showLoading({
    title: '加载中',
  })
  util.request(util.API.getHomeParkStatistic(),{queryType:_this.queryType,startTime:'1921-07',endTime:'2021-07',groupId:'1378190227727616'}, function (res) {
    if (res.error) {
      return;
    }
    if (res.result.code === 1) {
      wx.hideLoading();
      _this.setData({
        compelteRateRes:res.result.result
      })
    } else {
      wx.showToast({
        title: res.result.message
      })
    }
  })
},
getParkStatistic: function (){
  let _this = this;
  wx.showLoading({
    title: '加载中',
  })
  util.request(util.API.getHomeParkStatistic(),{startTime:'1921-07',endTime:'2021-07',groupId:'1378190227727616',isAll:true}, function (res) {
    if (res.error) {
      return;
    }
    if (res.result.code === 1) {
      wx.hideLoading();
      _this.setData({
        homeParkStatistic: res.result.result,
      })
    } else {
      wx.showToast({
        title: res.result.message
      })
    }
  })
},
 getBarOption() {
  let _this =this;
  return {
    tooltip: {},
    legend: {
      data: ['本周期', '上年同期']
    },
    xAxis: [
      {
        type: 'category',
        data: ['税收总额', '新增户税收', '企业注册数', '纳税企业数', '新增质量户'],
        axisPointer: {
          type: 'shadow'
      },
      axisLabel: {
        interval:0,
        show: true,
        textStyle: {
            color: '#A6B6C6',
            fontSize: 12
        },
        formatter: function (value,index) {
          let key=typeArray[index];
          let rateValue = 100 *_this.data.compelteRateRes[key].rate;
          let res=`{text|${value}}\n{${rateValue >= 0?'rateRed':'rateGreen'}|${ isNaN(rateValue)? '--' : (rateValue<0?rateValue:'+'+rateValue)+'%'}}\n`
          res+=`{textItem|${_this.data.compelteRateRes[key].currentData?_this.data.compelteRateRes[key].currentData+typeObj[key].unit:'--'}}\n`;
          res+=`{textItem|${_this.data.compelteRateRes[key].lastData?_this.data.compelteRateRes[key].lastData+typeObj[key].unit:'--'}}`;
          return res
      },
        lineHeight:20,
        fontSize:12,
        rich: {
            textItem:{
                fontSize:12,
                color:'#A6B6C6'
            },
            text:{
                fontSize:12,
                color:'#A6B6C6'
            },
            rateRed: {
                color: '#bd4b29'
            },
            rateGreen: {
                color: '#43895E'
            }
        }
    },
    axisLine: {
        // X轴线的颜色
        lineStyle: {
            color: '#A6B6C6',
            width: 1
        }
    },
    axisTick: {
        show: false
    }
      }
    ],
    yAxis: [
      {
        splitLine: {
            //控制刻度横线的显示
            show: false
        },
        type: 'value',
        min: 0,
        nameTextStyle: {
            fontSize: 11,
            align:'right'
        },
        name: '单位: 万元',
        axisLabel: {
            show: true,
            textStyle: {
                color: '#A6B6C6',
                fontSize: 11
            }
        },
        axisLine: {
            // Y轴线的颜色、和轴线的宽度
            show: false,
            lineStyle: {
                color: '#A6B6C6',
                width: 1
            }
        },
        axisTick: {
            show: false
        }
    },
    {
      splitLine: {
          show: true,
          lineStyle: {
              type: 'dashed'
          }
      },
      type: 'value',
      min: 0,
      name: '单位: 户',
      nameTextStyle: {
          fontSize: 12
      },
      axisLabel: {
          show: true,
          textStyle: {
              color: '#A6B6C6',
              fontSize: 12
          }
      },
      axisLine: {
          show: false,
          lineStyle: {
              color: '#A6B6C6',
              width: 1
          }
      },
      axisTick: {
          show: false
      }
  },
    ],
    series: [
      {
          name: '本周期金额',
          type: 'bar',
          yAxisIndex: 0,
          stack: 'current',
          data: [
            _this.data.compelteRateRes.totalTax.currentData,
            _this.data.compelteRateRes.parkEnterpriseTaxNewNum.currentData,
              null,
              null,
              null
          ],
          barWidth: 11, //柱图宽度
          itemStyle: {
              //双Y轴A柱的柱体颜色
              normal: {
                  color: '#1166FF'
              }
          }
      },
      {
          name: '上年同期金额',
          type: 'bar',
          yAxisIndex: 0,
          stack: 'last',
          data: [
            _this.data.compelteRateRes.totalTax.lastData,
            _this.data.compelteRateRes.parkEnterpriseTaxNewNum.lastData,
              null,
              null,
              null
          ],
          barWidth: 11, //柱图宽度
          itemStyle: {
              //双Y轴A柱的柱体颜色
              normal: {
                  color: '#FF9F24'
              }
          }
      },
      {
          name: '本周期用户',
          type: 'bar',
          yAxisIndex: 1,
          stack: 'current',
          data: [
              null,
              null,
               _this.data.compelteRateRes.parkEnterpriseNum.currentData,
               _this.data.compelteRateRes.parkEnterpriseTaxNum.currentData,
              _this.data.compelteRateRes.parkEnterpriseQualityNum.currentData
          ],
          barWidth: 11, //柱图宽度
          itemStyle: {
              //双Y轴A柱的柱体颜色
              normal: {
                  color: '#1166FF'
              }
          }
      },
      {
          name: '上年同期用户',
          type: 'bar',
          yAxisIndex: 1,
          stack: 'last',
          data: [
              null,
              null,
              _this.data.compelteRateRes.parkEnterpriseNum.lastData,
              _this.data.compelteRateRes.parkEnterpriseTaxNum.lastData,
              _this.data.compelteRateRes.parkEnterpriseQualityNum.lastData
          ],
          barWidth: 11, //柱图宽度
          itemStyle: {
              //双Y轴B柱的柱体颜色
              normal: {
                  color: '#FF9F24'
              }
          }
      }
  ]
  };
},
  onReady() {
  }
});
